package com.rbcs.base.tripgrade.webapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.base.model.TripGrade;
import com.rbcs.base.tripgrade.service.TripGradeManager;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;

@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class TripGradeAction extends
		DefaultCrudAction<TripGrade, TripGradeManager> {

	/**
	 * 标准管理列表
	 */
	public String index() {
		StringBuffer hql = new StringBuffer("from TripGrade tg where 1=1");
		List<Object> args = new ArrayList<Object>();
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and tg.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		if (StringUtils.isNotBlank(getModel().getCitys())) {
			hql.append(" and tg.citys like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getCitys()));
		}
		page = PageUtil.getPage(getPageNo(), getPageSize());
		page = getManager().pageQuery(page, hql.toString(), args.toArray());
		if (StringUtils.isNotBlank(getModel().getCitys())) {// 显红
			String newCitys = "<font color='red'>" + getModel().getCitys()
					+ "</font>";
			for (Object o : page.getData()) {
				TripGrade tg = (TripGrade) o;
				tg.setCitys(tg.getCitys().replace(getModel().getCitys(),
						newCitys));
			}
		}
		restorePageData(page);
		return INDEX;
	}

	@Override
	public String save() {
		// 判断是否有重复名称
		if (getManager().getDao().exists(getModel(), "name")) {
			addActionError("级别名称已存在！");
			return INPUT;
		}
		return super.save();
	}
	
	public String selectTripGrade(){
		StringBuffer hql = new StringBuffer("from TripGrade tg where 1=1");
		List<Object> args = new ArrayList<Object>();
		if (StringUtils.isNotBlank(getModel().getName())) {
			hql.append(" and tg.name like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getName()));
		}
		if (StringUtils.isNotBlank(getModel().getCitys())) {
			hql.append(" and tg.citys like ?");
			args.add(MatchMode.ANYWHERE.toMatchString(getModel().getCitys()));
		}
		page = PageUtil.getPage(getPageNo(), getPageSize());
		page = getManager().pageQuery(page, hql.toString(), args.toArray());
		if (StringUtils.isNotBlank(getModel().getCitys())) {// 显红
			String newCitys = "<font color='red'>" + getModel().getCitys()
					+ "</font>";
			for (Object o : page.getData()) {
				TripGrade tg = (TripGrade) o;
				tg.setCitys(tg.getCitys().replace(getModel().getCitys(),
						newCitys));
			}
		}
		restorePageData(page);
		return "select";
	}
}
